package net.uhuijia.user.useraddress.service.impl;

import net.uhuijia.enums.ErrorCodeEnum;
import net.uhuijia.exception.BusinessException;
import net.uhuijia.sys.area.clientservice.SysAreaClientService;
import net.uhuijia.sys.area.persistence.model.SysArea;
import net.uhuijia.user.useraddress.service.UserAddressService;
import net.uhuijia.user.usermall.persistence.model.UserMall;
import net.uhuijia.vo.user.useraddress.returnvo.AddressListVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import net.uhuijia.appbase.service.CrudService;
import net.uhuijia.user.useraddress.persistence.dao.UserAddressDao;
import net.uhuijia.user.useraddress.persistence.model.UserAddress;
import net.uhuijia.user.useraddress.persistence.model.UserAddressExample;
import net.uhuijia.user.useraddress.persistence.model.UserAddressExample.Criteria;

import java.util.List;


@Service
@Transactional
public class UserAddressServiceImpl
     extends CrudService<UserAddressDao,UserAddress,UserAddressExample> implements UserAddressService{
    @Autowired
    private UserAddressDao userAddressDao;
    @Autowired
    private SysAreaClientService sysAreaClientService;

    @Override
    public UserAddress addAddress(UserMall userMall, String receiveName, String receivePhone, String provinceId, String cityId, String regionId, String detailaAddress,String isDefault) {
        if(!StringUtils.isNoneBlank(receiveName,receivePhone,provinceId,cityId,regionId))
            throw new IllegalArgumentException();
        //效验省市区是否存在
        SysArea province = this.sysAreaClientService.getAreaById(provinceId);
        SysArea city = this.sysAreaClientService.getAreaById(cityId);
        SysArea region = this.sysAreaClientService.getAreaById(regionId);
        if(province!=null && city!=null && region!=null){
            //查询用户默认收获地址
            UserAddress defaultAddress = this.getUserDefaultAddress(userMall.getId());
            //判断用户是否有默认收货地址
            if(defaultAddress!=null){
                //有 判断isDefault是否是默认值 是 替换
                if("1".equals(isDefault)){
                    //取消默认
                   defaultAddress.setIsDefault("0");
                   this.edit(defaultAddress);
                }else
                    isDefault="0";
            }else{
                isDefault="1";// 否 将当前设置成默认
            }
            UserAddress userAddress = this.addUserAddress(userMall.getId(),receiveName,receivePhone,provinceId,province.getRegionName(),cityId,city.getRegionName(),regionId,region.getRegionName(),detailaAddress,isDefault);
            return userAddress;
        }else{
            throw new BusinessException(ErrorCodeEnum.area_not_exist_error);
        }
    }
    /**
     * @Author xzj
     * @Description //查询用户默认收获地址
     * @Date  2018/10/9
     * @Param [userId]
     * @return net.uhuijia.user.useraddress.persistence.model.UserAddress
     **/
    @Override
    public UserAddress getUserDefaultAddress(String userId) {
        UserAddress userAddress = this.mapper.getUserDefaultAddress(userId);
        return userAddress;
    }

    public UserAddress general(String userId, String receiveName, String receivePhone, String provinceId, String province, String cityId, String city, String regionId, String region, String detailaAddress){
        UserAddress userAddress = new UserAddress();
        userAddress.setUserId(userId);
        userAddress.setReceiverName(receiveName);
        userAddress.setReceiverPhone(receivePhone);
        userAddress.setProvinceId(provinceId);
        userAddress.setProvinceName(province);
        userAddress.setCityId(cityId);
        userAddress.setCityName(city);
        userAddress.setRegionId(regionId);
        userAddress.setRegionName(region);
        userAddress.setAddress(detailaAddress);
        return userAddress;
    }

    @Override
    public UserAddress addUserAddress(String userId, String receiveName, String receivePhone, String provinceId, String province, String cityId, String city, String regionId, String region, String detailaAddress, String isDefault) {
        UserAddress userAddress = this.general(userId,receiveName,receivePhone,provinceId,province,cityId,city,regionId,region,detailaAddress);
        userAddress.setIsDefault(isDefault);
        this.add(userAddress);
        return userAddress;
    }

    @Override
    public List<AddressListVo> findAddressList(UserMall userMall, UserAddressExample example) {


        return null;
    }

    @Override
    public List<UserAddress> findUserAddressListByUserIdandPage(UserMall userMall, UserAddressExample example) {
        Criteria criteria = example.createCriteria();

        return null;
    }
}